import requests,json
import pandas as pd
lin_key = "13140ae1a60e2e237990fd891dbe9826"
#用函数调用地理编码
def geocode(key,address,city=None,batch='false',sig=None,output='json',callback=None):
    """"获取好的Api的地理编码信息"""
    url='https://restapi.amap.com/v3/geocode/geo?parameters'
    params={
    'key':lin_key,
    'address':address,
    'city':city,
    'batch':batch,
    'sig':sig,
    'output':output,
    'callback':callback
    }
    response=requests.get(url,params=params)
    results=response.json()['geocodes'][0]['location']
    # return
    return results
#逆地理编码
def regeo(key,location,poitype=None,radius='1000',extensions='base',batch='false',roadlevel=None,sig=None,
          output='json',callback=None,homeorcorp='0'):
    # 通过经纬度来判断位置
    url='https://restapi.amap.com/v3/geocode/regeo?parameters'
    params={
        'key':lin_key,
        'location':location,
        'poitype':poitype,
        'radius':radius,
        'extensions':extensions,
        'batch':batch,
        'sig':sig,
        'output':output,
        'callback':callback,
        'homeorcorp':homeorcorp
    }
    response=requests.get(url,params)
    results=response.json()
    return results
#路径规划之步行规划
def walking(lin_key,origin,location,sig=None,output='json',callback=None):
    walking_url='https://restapi.amap.com/v3/direction/walking?parameters'
    params={
        'key':lin_key,
        'origin':origin,
        'destination':location,
        'sig':sig,
        'output':output,
        'callback':callback,     
    }
    response=requests.get(walking_url,params)
    results=response.json()
    # return
    return results
#路径规划之公交规划
def transit(origin,location,city,cityid=None,extensions='base',strategy=0,nightflag=0,
            date=None,time=None,sig=None,output='json',callback=None):    #公交路径规划    
    transit_url='https://restapi.amap.com/v3/direction/transit/integrated?parameters'    
    params={
        'key':lin_key,
        'origin':origin,
        'destination':location,
        'city':city,
        'cityid':cityid,
        'extensions':extensions,
        'strategy':strategy,
        'nightflag':nightflag,
        'date':date,
        'time':time,
        'sig':sig,
        'output':output,
        'callback':callback            }    
    response=requests.get(transit_url,params)    
    results=response.json()     
    return results
#路径规划之驾车规划
def driving(lin_key,origin,destination,extensions):
    #驾车路径规划
    url='https://restapi.amap.com/v3/direction/driving?parameters'
    params={
        'key':lin_key,
        'origin':origin,
        'destination':destination,
        'extensions':extensions   
    }
    response=requests.get(url,params)
    results=response.json()
    return results
#路径规划之骑行规划
def bicycling(key,origin,destination):
    #骑车路径规划
    bicycling_url='https://restapi.amap.com/v4/direction/bicycling?parameters'
    params={
        'key':lin_key,
        'origin':origin,
        'destination':destination
    }
    response=requests.get(bicycling_url,params)
    results=response.json()
    return results
#路径规划之货车路径
def truck(key,origin,destination,size):
    #货车路径规划
    url='https://restapi.amap.com/v4/direction/truck?parameters'
    params={
        'key':lin_key,
        'origin':origin,
        'destination':destination, 
        'size':size
    }
    response=requests.get(url,params)
    results=response.json()
    # return
    return results
#路径规划距离测量
def distance(origins,destination,type=''):
    distance_url='https://restapi.amap.com/v3/distance?parameters'
    key='18ef7dd6aeefc21dade574262c00f213'
    params={
        'key':lin_key,
        'origins':origins,
        'destination':destination, 
        'type':type
      
    }
    response=requests.get(distance_url,params)
    results=response.json()
    return results
#行政区域查询
def district(lin_key,keywords=None,subdistrict='1',page='1',offset='20',extensions='base',filter='',callback=None,output='json'):
    district_url='https://restapi.amap.com/v3/config/district?parameters'
    params={
        'key':lin_key,
        'keywords':keywords,
        'subdistrict':subdistrict,
        'extensions':extensions,
        'page':page,
        'offset':offset,
        'filter':filter,
        'callback':callback,
        'output':output     
    }
    response=requests.get(district_url,params)
    results=response.json()
    return results

#搜索POI
def text(keywords,city='',extensions='',citylimit=''):
    #关键词搜索
    url='https://restapi.amap.com/v3/place/text?parameters'
    params={
        'key':lin_key,
        'keywords':keywords,
        'city':city,
        'extensions':extensions,
        'citylimit':citylimit
      
    }
    response=requests.get(url,params)
    results=response.json()
    # return
    return results

#搜索POI之周边搜索
def around(location,keywords=''):
    #周边搜索
    url='https://restapi.amap.com/v3/place/around?parameters '
    key='18ef7dd6aeefc21dade574262c00f213'
    params={
        'key':key,
        'keywords':keywords,
        'location':location
      
    }
    response=requests.get(url,params)
    results=response.json()
    # return
    return results
#搜索POI之关键字搜索
def text(keywords,city='',extensions='',citylimit=''):
    #关键词搜索
    url='https://restapi.amap.com/v3/place/text?parameters'
    params={
        'key':lin_key,
        'keywords':keywords,
        'city':city,
        'extensions':extensions,
        'citylimit':citylimit
      
    }
    response=requests.get(url,params)
    results=response.json()
    # return
    return results

#搜索POI之多边形搜索
def poly(key,polygon,keywords=None,types=None,offset='20',page='1',extensions='base',sig=None,output='json',callback=None):
    url='https://restapi.amap.com/v3/place/polygon?parameters '
    params={
        'key':lin_key,
        'polygon':polygon,
        'keywords':keywords,
        'types':types,
        'offset':offset,
        'page':page,
        'extensions':extensions,
        'sig':sig,
        'output':output,
        'callback':callback     
    }
    response=requests.get(url,params)
    results=response.json()
    # return
    return results

#id查询
def detail(id):
    #id查询搜索
    detail_url='https://restapi.amap.com/v3/place/polygon?parameters '
    params={
        'key':lin_key,
        'id':id,
      
    }
    response=requests.get(detail_url,params)
    results=response.json()
    # return
    return results

#ip定位
def ip(key,id):
    # ip定位
    url='https://restapi.amap.com/v3/ip?parameters'
    params={
        'key':lin_key,
        'id':id
      
    }
    response=requests.get(url,params)
    results=response.json()
    # return
    return results

#静态地图
from PIL import Image
from io import BytesIO 
def staticmap(key,location,zoom):
    ###静态地图###
    url="https://restapi.amap.com/v3/staticmap?parameters"
    location="113.350961,23.131577"
    params ={
        "key":lin_key,
        "location":location,
        "zoom":zoom
    }
    r = requests.get(url,params)
    results = Image.open(BytesIO(r.content))
    return results
#坐标转换
def convert(key,locations,coordays='',sig='',output=''):
    url='https://restapi.amap.com/v3/assistant/coordinate/convert?parameters'
    params={
        'key':lin_key,
        "locations":locations,
        'coordays':coordays,
        'sig':sig,
        'output':output
    }
    response=requests.get(url,params)
    r=response.json()
    return r
#天气查询
def weather(key,city,extensions='base',output=''):
    # 天气查询
    url='https://restapi.amap.com/v3/weather/weatherInfo?parameters'
    params={
        'key':lin_key,
       'city':city,
        'extensions':extensions,
        'output':output
    }
    response=requests.get(url,params)
    r=response.json()
    return r

#输入提示
def input(key,keywords,type=None,location=None,city=None,
          citylimit='false',datatype=None,sig=None,output='json',callback=None):
    # 输入提示
    url='https://restapi.amap.com/v3/assistant/inputtips?parameters'
    params={
        'key':lin_key,
        'keywords':keywords,
        'type':type,
        'location':location,
        'city':city,
        'citylimit':citylimit,
        'datatype':datatype,
        'sig':sig,
        'output':output,
        'callback':callback
    }
    response=requests.get(url,params)
    r=response.json()
    return r

#交通态势
def traffic(key,rectangle,level='5',extensions='base',sig=None,output='json',callback=None):
    url='https://restapi.amap.com/v3/traffic/status/rectangle?parameters'
    params={
        'key':lin_key,
        'rectangle':rectangle,
        'level':level,
        'extensions':extensions,
        'sig':sig,
        'output':output,
        'callback':callback,
    }
    response=requests.get(url,params)
    r=response.json()
    return r

#交通态势接口计划将于2020年12月31日下线，目前不再对新用户开放。如需合作请通过工单进行商务咨询。

